from django.db import models
from django.contrib.flatpages.models import FlatPage
from django.utils.log import logger
try:
    from mptt.models import MPTTModel
    class ExtendedFlatPage(MPTTModel, FlatPage):
        parent = models.ForeignKey('ExtendedFlatPage', null=True, blank=True, default=None, related_name="children", help_text="Hierarchical parent page (if any)")
        order = models.PositiveIntegerField()
        inmenu = models.BooleanField(default=False)
        class Meta:
            ordering = ['order']
            order_with_respect_to = 'parent'
            verbose_name = 'page'
            verbose_name_plural = 'pages'
    
        class MPTTMeta:
            left_attr = 'mptt_left'
            right_attr = 'mptt_right'
            level_attr = 'mptt_level'
            order_insertion_by = ['title']
    
        def __unicode__(self):
            return self.url    
except ImportError, e:
    logger.debug('MPTTModel not found')
    class ExtendedFlatPage(FlatPage):
        parent = models.ForeignKey('ExtendedFlatPage', null=True, blank=True, default=None, related_name="children", help_text="Hierarchical parent page (if any)")
        order = models.PositiveIntegerField()
        inmenu = models.BooleanField(default=False)
        class Meta:
            ordering = ['order']
            order_with_respect_to = 'parent'
            verbose_name = 'page'
            verbose_name_plural = 'pages'
    
        def __unicode__(self):
            return self.url    
    
    
